require( '$d/css/common.less' );
require( './../style.less' );
define( [
  'require',
  'jquery',
  'message',
  'constants',
  'echarts',
  'bigwe_utils',
  'header',
], function( require, $, message, constants, echarts ) {
  var details = {
    u: '/_topic/topic_info',
    data: {
      cd: $( '.art_cd' ).val(),
      category: $( '.art_cd' ).data( 'category' ),
      source: $( '.art_cd' ).data( 's' ),
    },
    callBack: deploy_info,
  }

  function myAjax( opt ) {
    message.show( '正在加载...' );
    $.ajax( {
      type: 'post',
      url: opt[ 'u' ],
      data: opt[ 'data' ],
      success: function( data ) {
        if ( $.isFunction( opt[ 'callBack' ] ) ) {
          opt[ 'callBack' ]( data );
        }
      },
      error: function( XMLHttpRequest, textStatus, errorThrown ) {
        console.log( textStatus );
        console.log( XMLHttpRequest.status );
        console.log( XMLHttpRequest.readyState );
      },
      complete: function() {
        message.hide();
      }
    } );
  }

  function deploy_info( data ) {
    var d = typeof data === 'string' ? $.parseJSON( data ) : data;
    var _data = d.data.topic_info;
    var n = '';

    ( _data.read_value != '0' ) ? ( n = _data.read_value ) : ( n = _data.index_value );

    var icon = '';
    if ( _data.level == 0 ) {
      icon = 'icon-jian'
    } else if ( _data.level == 1 ) {
      icon = 'icon-iconfontmovetoup green'
    } else {
      icon = 'icon-iconfontmovetodown'
    }

    var htm = '';
    htm += '<div class="ranking_info"><p><span class="iconfont icon-statistic"></span>' + $( '.art_cd' ).data( 'category' ) + '排名</p>';

    if ( _data.rank ) {
      htm += '<p>NO.' + _data.rank + '<span class="iconfont ' + icon + '"></span></p>'
    } else {
      htm += '<p class="no_dt">--&nbsp;--</p>'
    }

    htm += '</div><div class="follow_info"><p><span class="iconfont icon-collected"></span>';

    if ( _data.source == 'd.weibo.com' ) {
      htm += '新增关注'
    } else {
      htm += '搜索指数'
    }

    if ( n.toString().length > 5 && n.toString().length < 9 ) {
      n = Math.round( ( n / 10000 ) * 10 ) / 10 + '万';
    } else if ( n.toString().length >= 9 ) {
      n = Math.round( ( n / 100000000 ) * 10 ) / 10 + '亿';
    }

    htm += '</p><p>' + n + '</p></div><div id="diagram" class="diagram"></div>';
    $( '.deploy_info' ).append( htm );
    d.data.sort[ 0 ][ 'source' ] = _data.source;
    echarts_t( d.data.sort );
  }

  function echarts_t( json ) {
    var diagram = par_sort( json ),
      e_t = '';
    if ( json[ 0 ].source == 'top.baidu.com' ) {
      if ( diagram.x.length > 1 ) {
        e_t = diagram.x[ 0 ] + ' 至 ' + diagram.x[ diagram.x.length - 1 ] + ' 搜索指数趋势';
      } else {
        e_t = diagram.x[ 0 ] + ' 搜索指数趋势';
      }
    } else {
      if ( diagram.x.length > 1 ) {
        e_t = diagram.x[ 0 ] + ' 至 ' + diagram.x[ diagram.x.length - 1 ] + ' 新增关注趋势';
      } else {
        e_t = diagram.x[ 0 ] + ' 新增关注趋势';
      }
    }

    var myChart = echarts.init( document.getElementById( 'diagram' ) );
    // 图表使用-------------------
    var option = {
      legend: {
        orient: 'horizontal',
        x: 'center',
        y: 'bottom',
        selectedMode: false,
        data: [ {
          name: e_t,
          icon: 'circle',
          textStyle: {
            fontSize: 15,
            color: '#333'
          },
        }, ]
      },
      tooltip: {
        trigger: 'axis',
      },
      xAxis: [ {
        data: diagram.x,
      } ],
      yAxis: [ {
        boundaryGap: [ 0, 0.1 ],
      } ],
      series: [ {
        name: e_t,
        type: 'line',
        data: diagram.y,
      } ],
      color: [ '#4a90e2' ],
    };
    myChart.setOption( option );
  }

  // function par_sort(json) {
  //     var x = [],
  //         y = [];
  //     for (i in json) {
  //         x[i] = new Date(json[i].data_time).format('yyyy-MM-dd');
  //         y[i] = json[i].interfix_value;
  //     }
  //     return {
  //         x: x.reverse(),
  //         y: y.reverse(),
  //     };
  // }

  function par_sort( json ) {
    var d = [];
    for ( i in json ) {
      d[ json[ i ].day_code ] = json[ i ].interfix_value;
    }

    var date = new Date();
    var day_mm = 24 * 60 * 60 * 1000,
      x = [],
      y = [];

    for ( i = 0; i < 7; i++ ) {
      var t = new Date( date - ( i * day_mm ) );
      var year = t.getFullYear(),
        min = t.getMonth() + 1,
        day = t.getDate();
      x[ i ] = t.format( 'yyyy-MM-dd' );

      if ( d[ t.format( 'yyyyMMdd' ) ] ) {
        y[ i ] = d[ t.format( 'yyyyMMdd' ) ];
      } else {
        y[ i ] = 0;
      }
    }
    return {
      x: x.reverse(),
      y: y.reverse(),
    };
  }
  $( '.search_area .search_item' ).eq( 0 ).addClass( 'selected' );
  myAjax( details );
} )
